package net.wwang.open.global;

import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * 全局返回体
 *
 * @author WangWei
 * @date 2020/8/12
 */
@NoArgsConstructor
@Data
public class ResponseResult implements Serializable {
	private static final long serialVersionUID = 683542009879967860L;
	private boolean success;
	private int code;
	private String message;
	private Object data;

	public ResponseResult(ResponseState responseState) {
		this.success = responseState.isSuccess();
		this.code = responseState.getCode();
		this.message = responseState.getMessage();
	}

	public static ResponseResult SUCCESS() {
		return new ResponseResult(ResponseState.SUCCESS);
	}

	public static ResponseResult SUCCESS(String message) {
		ResponseResult result = new ResponseResult(ResponseState.SUCCESS);
		result.setMessage(message);
		return result;
	}

	public static ResponseResult CREATED() {
		return new ResponseResult(ResponseState.CREATED);
	}

	public static ResponseResult CREATED(String message) {
		ResponseResult result = new ResponseResult(ResponseState.CREATED);
		result.setMessage(message);
		return result;
	}

	public static ResponseResult FAILED() {
		return new ResponseResult(ResponseState.FAILED);
	}

	public static ResponseResult FAILED(String message) {
		ResponseResult result = new ResponseResult(ResponseState.FAILED);
		result.setMessage(message);
		return result;
	}

	public static ResponseResult UN_AUTHORIZED() {
		return new ResponseResult(ResponseState.UN_AUTHORIZED);
	}

	public static ResponseResult UN_AUTHORIZED(String message) {
		ResponseResult result = new ResponseResult(ResponseState.UN_AUTHORIZED);
		result.setMessage(message);
		return result;
	}

	public static ResponseResult FORBIDDEN() {
		return new ResponseResult(ResponseState.FORBIDDEN);
	}

	public static ResponseResult FORBIDDEN(String message) {
		ResponseResult result = new ResponseResult(ResponseState.FORBIDDEN);
		result.setMessage(message);
		return result;
	}

	/**
	 * 构造数据
	 *
	 * @param data
	 * @return
	 */
	public ResponseResult setData(Object data) {
		this.data = data;
		return this;
	}

	public ResponseResult(boolean success, int code, String message) {
		this.success = success;
		this.code = code;
		this.message = message;
	}

	public ResponseResult(boolean success, int code, String message, Object data) {
		this.success = success;
		this.code = code;
		this.message = message;
		this.data = data;
	}
}
